-- SPDX-License-Identifier: GPL-3.0-or-later

--技能选了牌就报错，目前没看懂问题在哪
local VslUtil = require "packages/VirtualSlash/utility"

local v_yangge = fk.CreateSkill {
  name = "v_yangge",
  tags = {  },
}

v_yangge:addEffect(fk.EventPhaseStart, {
  --赋予支援型技能定义
  anim_type = "support",
  --触发条件：触发时机的角色为遍历到的角色、遍历到的角色具有本技能，遍历到的角色处于结束阶段
  --         本回合未造成伤害。
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(v_yangge.name) 
    and player.phase == Player.Finish then
      local damage_or_not = #player.room.logic:getActualDamageEvents(1, function (e)
        return e.data.from == player
      end, Player.HistoryTurn)
      return damage_or_not == 0
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local peach=room:askToCards(player,{
      max_num=1,
      min_num=1,
      include_equip=false,
      skill_name=v_yangge.name,
      cancelable=true,
      prompt="#v_yangge_ask",
    })[1]
    if peach then
      room:throwCard(peach, v_yangge.name, player)
      local god_salvation = Fk:cloneCard("god_salvation")
      god_salvation.skillName = v_yangge.name
      local tos = {}
      for _,p in ipairs(room:getAlivePlayers()) do
        if not player:isProhibited(p, god_salvation) then
      	  table.insert(tos, p)
        end
      end
      if not VslUtil.is_jilei(player, god_salvation) then
      	room:useCard{
          card = god_salvation,
          from = player,
          tos = tos,
        }
      end
    else
      player:turnOver()
      local savage_assault = Fk:cloneCard("savage_assault")
      savage_assault.skillName = v_yangge.name
      local tos = {}
      for _,p in ipairs(room:getOtherPlayers(player)) do
        if not player:isProhibited(p, savage_assault) then
      	  table.insert(tos, p)
        end
      end
      if not VslUtil.is_jilei(player, savage_assault) then
      	room:useCard{
          card = savage_assault,
          from = player,
          tos = tos,
        }
      end
    end
  end,
})

return v_yangge